package com.douwong.student.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.douwong.student.model.AnswerRecord;
import com.douwong.student.model.AnswerRecordType;
import com.douwong.student.model.QuizResultsInfo;

import java.util.ArrayList;


public class AnswerRecordDBService extends BDBaseService<AnswerRecord> {
//	private DBHelper dbHelper = null;
	private String MTAG = "AnswerRecordDBService";

	public AnswerRecordDBService(Context context) {
		super(context);
//		dbHelper = new DBHelper(context);
	}

	public void closeBD() {
		dbHelper.close();
	}

	public int addAnswerRecord(AnswerRecord recordModel, String UserId) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		try {
			db.execSQL(
					"insert into answer_record(TopicName,TopicType,TopicTeacher,TopicTimes,TopicStar,filePath,UserId,trueAnswer,StudentAnswer,optionStr) values(?,?,?,?,?,?,?,?,?,?)",
					new Object[] { recordModel.getTitleName(),
							recordModel.getType(), recordModel.getSubject(),
							recordModel.getCreateTime(),
							recordModel.getAwardCount(),
							recordModel.getFilePath(), UserId,
							recordModel.getTrueAnswer(),
							recordModel.getStudentAnswer(),
							recordModel.getOptionStr()
					});
			Log.e(MTAG, "插入成功");
			int id = 0;
			Cursor cursor = db.rawQuery(
					"select id from answer_record where TopicName=?",
					new String[] { recordModel.getTitleName() });
			if (cursor != null) {
				if (cursor.moveToFirst()) {
					id = cursor.getInt(0);
				}
			}
			int ids = id;
			cursor.close();
			return id;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		} finally {
			db.close();
		}
	}

	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub
	}

	public boolean updateRecord(String Userid, int id,
								QuizResultsInfo quizResultsInfo, String StudentAnswer) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		try {
			db.execSQL(
					"update answer_record set TopicStar=?,trueAnswer=?,StudentAnswer=?,Correctrate=? where UserId=? and id=? ",
					new Object[] { Integer.parseInt(quizResultsInfo.Flower),
							quizResultsInfo.Solution,StudentAnswer,quizResultsInfo.Accuracy,
							Userid, id });
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {

			db.close();
		}
	}

	@Override
	public AnswerRecord query(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	// 查询全部答题数据
	public ArrayList<AnswerRecord> allAnswerRecord(String userId,
												   ArrayList<AnswerRecord> recordModels) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cu = db.rawQuery("select * from answer_record where UserId=? order by id desc",
				new String[] { userId });
		int i = cu.getColumnCount();
		Log.d(MTAG, cu.getColumnCount() + "");

		while (cu.moveToNext()) {
			AnswerRecord recordModel = new AnswerRecord();
			recordModel.setTitleName(cu.getString(1));
			if (cu.getString(2).equals("Single")) {
				recordModel.setType(AnswerRecordType.Single);// 单选题
			}
			if (cu.getString(2).equals("Multiple")) {

				recordModel.setType(AnswerRecordType.Multiple);// 多选
			}
			if (cu.getString(2).equals("Judge")) {
				recordModel.setType(AnswerRecordType.Judge);// 判断题
			}
			recordModel.setSubject(cu.getString(3));
			recordModel.setCreateTime(cu.getString(4));
			recordModel.setAwardCount(cu.getInt(5));
			recordModel.setFilePath(cu.getString(6));
			recordModel.setStudentAnswer(cu.getString(8));
			recordModel.setTrueAnswer(cu.getString(9));
			recordModel.setOptionStr(cu.getString(10));
			recordModel.setCorrectRate(cu.getString(11));
			recordModels.add(recordModel);
		}

		return recordModels;
	}

	// 查询答题类型
	public ArrayList<AnswerRecord> AnswerRecordType(String userId,
			ArrayList<AnswerRecord> recordModels, String answerType) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cu = db.rawQuery(
				"select * from answer_record where UserId=? and TopicType=? order by id desc ",
				new String[] { userId, answerType });
		int i = cu.getCount();
		Log.d(MTAG, cu.getColumnCount() + "");

		while (cu.moveToNext()) {
			AnswerRecord recordModel = new AnswerRecord();
			recordModel.setTitleName(cu.getString(1));
			if (cu.getString(2).equals("Single")) {
				recordModel.setType(AnswerRecordType.Single);// 单选题
			}
			if (cu.getString(2).equals("Multiple")) {

				recordModel.setType(AnswerRecordType.Multiple);// 多选
			}
			if (cu.getString(2).equals("Judge")) {
				recordModel.setType(AnswerRecordType.Judge);// 判断题
			}
			recordModel.setSubject(cu.getString(3));
			recordModel.setCreateTime(cu.getString(4));
			recordModel.setAwardCount(cu.getInt(5));
			recordModel.setFilePath(cu.getString(6));
			recordModel.setStudentAnswer(cu.getString(8));
			recordModel.setTrueAnswer(cu.getString(9));
			recordModel.setOptionStr(cu.getString(10));
			recordModel.setCorrectRate(cu.getString(11));
			recordModels.add(recordModel);
		}

		return recordModels;
	}

	public ArrayList<AnswerRecord> AnswerQuery(String UserId, String TopicType) {
		ArrayList<AnswerRecord> recordModels = new ArrayList<AnswerRecord>();
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cu = db.rawQuery(
				"select * from answer_record where UserId=? and TopicType = ?",
				new String[] { UserId, TopicType });

		while (cu.moveToNext()) {
			AnswerRecord recordModel = new AnswerRecord();
			recordModel.setTitleName(cu.getString(1));
			if (cu.getString(2).equals("0")) {
				recordModel.setType(AnswerRecordType.Single);// 单选题
			}
			if (cu.getString(2).equals("1")) {

				recordModel.setType(AnswerRecordType.Multiple);// 多选
			}
			if (cu.getString(2).equals("2")) {
				recordModel.setType(AnswerRecordType.Judge);// 判断题
			}
			recordModel.setSubject(cu.getString(3));
			recordModel.setCreateTime(cu.getString(4));
			recordModel.setAwardCount(cu.getInt(5));
			recordModel.setFilePath(cu.getString(6));
			recordModel.setStudentAnswer(cu.getString(8));
			recordModel.setTrueAnswer(cu.getString(9));
			recordModel.setOptionStr(cu.getString(10));
			recordModel.setCorrectRate(cu.getString(11));
			recordModels.add(recordModel);
		}


		return recordModels;
	}

	@Override
	public void add(AnswerRecord t) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(int id) {
		// TODO Auto-generated method stub

	}

}
